<?php
namespace App\Http\Controllers;

use App\Registration;
use Illuminate\Http\Request;
use App\Activity as ActModel;
use App\User;
use Illuminate\Support\Facades\Redirect;


/**
 * Created by PhpStorm.
 * User: hans
 * Date: 15/12/22
 * Time: 下午6:18
 */
class Activity extends Controller
{
    //活动状态
    public static $status = [
        '0' => '策划中',
        '1' => '报名中',
        '9' => '报名结束',
    ];

    /**
     * 活动
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $activities = ActModel::all();

        return view('activity.index', [
            'activities' => $activities,
            'status' => self::$status
        ]);
    }


    /**
     * 活动列表页
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function lists()
    {
        $activities = ActModel::all();

        return view('activity.list', [
            'activities' => $activities,
            'status' => self::$status
        ]);
    }


    /**
     * 报名登记页
     *
     * @param Request $request
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function register(Request $request, $id)
    {
        $act = ActModel::findOrFail($id);

        return view('activity.register', [
            'id' => $id,
            'act' => $act
        ]);
    }

    /**
     * 验证&保存报名信息
     *
     * @param Request $request
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function verify(Request $request, $id)
    {
        $act = ActModel::findOrFail($id);
        $r = $request->all();
        $msg = '';

        //有效性校验
        if ($r) {
            //兼容邮箱报名
            if (strpos($r['mobile'], '@') > 0) {
                $user = User::where('email', $r['mobile'])->first();
                if($user===null){
                    abort(422, '抱歉，您还没有完成身份认证~');
                }

                $r['mobile'] = $user->mobile;
            }

            if (!$this->hasUserRegistered($r['mobile'])) {
                abort(422, '抱歉，您还没有完成身份认证~');
                //return Redirect::to('/common/register')->with('mobile', $r['mobile']);
            }

            //检查是否允许报名
            $this->check($id, $r['mobile']);

            //保存报名信息
            $user = User::where('mobile', $r['mobile'])->firstOrFail();
            $reg = new Registration;
            $reg->activity_id = $r['id'];
            $reg->user_id = $user->id;
            $reg->save();

            //检查报名人数
            $reg_no = $act->countPartners();
            if ($act->max_no > 0 && $act->max_no <= $reg_no) {
                $act->endRegister();
            }
            //更新剩余名额
            $act->updateRemainingNo();

            return response()->redirectTo('/activity/' . $id);
        }

        //验证码校验
        /*
        if (isset($r['code'])) {

            if($r['code']=='123456'){
                $user = User::where('mobile', $r['mobile'])->firstOrFail();

                $reg = new Registration;
                $reg->activity_id = $r['id'];
                $reg->user_id = $user->id;
                $reg->save();

                //检查报名人数
                $reg_no = $act->countPartners();
                if ($act->max_no > 0 && $act->max_no <= $reg_no) {
                    $act->endRegister();
                }
                //更新剩余名额
                $act->updateRemainingNo();

                return response()->redirectTo('/activity/' . $id);
            } else {
                $msg = '验证码不正确';
            }
        }*/


        return view('activity.verify', [
            'act' => $act,
            'id' => $id,
            'mobile' => $r['mobile'],
            'msg' => $msg
        ]);
    }


    /**
     * 活动详情页
     *
     * @param Request $request
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function details(Request $request, $id)
    {
        $act = ActModel::findOrFail($id);
        $regs = Registration::where('activity_id', $id)->get();

        return view('activity.details', [
            'act' => $act,
            'regs' => $regs
        ]);
    }

    //检查报名条件
    private function check($id, $mobile)
    {
        $act = ActModel::findOrFail($id);

        //检查活动状态
        switch ($act->status) {
            case 0:
                abort(422, '别着急，活动正在策划中~');
                break;
            case 9:
                abort(422, '来晚啦，报名已结束！');
                break;
        }

        //检查是否已报名
        $user = User::where('mobile', $mobile)->firstOrFail();

        $act_registered = Registration::whereRaw('activity_id=? and user_id=?', [$id, $user->id])->count();
        if ($act_registered) {
            abort(422, '这个活动，您已经报过名啦~');
        }

        //检查报名人数
        $reg_no = $act->countPartners();
        if ($act->max_no > 0 && $act->max_no <= $reg_no) {
            abort(422, '活动太火爆啦，名额已满，看看其他活动吧~');
        }

        return true;
    }

    //检查是否注册
    private function hasUserRegistered($mobile)
    {
        $count = User::where('mobile', $mobile)->count();

        return boolval($count);
    }

}